<?php
add_action( 'add_meta_boxes', 'thumb_hide_box' );

/* Do something with the data entered */
add_action( 'save_post', 'thumb_hide_save_postdata' );
add_action( 'save_post', 'dt_post_thumbnail_opts_save' );
add_action( 'save_post', 'dt_blog_slider_layout_save' );

/* Adds a box to the main column on the Post and Page edit screens */
function thumb_hide_box() {
    // thumbnail custom options
    add_meta_box(
        'post_thumbnail_options',
        __( 'Custom thumbnail options', LANGUAGE_ZONE ),
        'dt_post_thumbnail_opts_box',
        'post',
        'advanced',
        'low'
    );
    
    add_meta_box(
        'post_thumbnail_options',
        __( 'Custom thumbnail options', LANGUAGE_ZONE ),
        'dt_post_thumbnail_opts_box',
        'dt_portfolio',
        'advanced',
        'low'
    );
    
    add_meta_box(
        'post_thumbnail_options',
        __( 'Custom thumbnail options', LANGUAGE_ZONE ),
        'dt_post_thumbnail_opts_box',
        'dt_gallery',
        'advanced',
        'low'
    );
    
    add_meta_box(
        'post_thumbnail_options',
        __( 'Custom thumbnail options', LANGUAGE_ZONE ),
        'dt_post_thumbnail_opts_box',
        'dt_our_clients',
        'advanced',
        'low'
    );
    
    add_meta_box(
        'hide_post_thumb',
        __( 'Hide featured image', LANGUAGE_ZONE ),
        'thumb_hide_inner_box',
        'post',
        'side',
        'low'
    );

    if ( isset($_GET['post']) ) {
        $post_id = $_GET['post'];
    } elseif ( isset($_POST['post_ID']) ) {
        $post_id = $_POST['post_ID'];
    } else
        return;

    $template_file = get_post_meta( $post_id, '_wp_page_template', TRUE );
    if( 'dt_blog_slider.php' == $template_file ) {
        add_meta_box(
            'dt_blog_slider_layout',
            __( 'Blog options', LANGUAGE_ZONE ),
            'dt_blog_slider_layout_inner_box',
            'page'
        );
    }
}

/* Prints the box content */
function thumb_hide_inner_box( $post ) {
    $box_name = 'dt_featured_image_options';
    
    $defaults = array(
        'his'    => false
    );
    $opts = get_post_meta( $post->ID, $box_name, true );
    $opts = wp_parse_args( $opts, $defaults );
    
    wp_nonce_field( plugin_basename( __FILE__ ), $box_name. '_nonce' );
    ?>
    
    <p>
        <label for="<?php echo $box_name; ?>_his">
        <input id="<?php echo $box_name; ?>_his" type="checkbox" name="<?php echo $box_name; ?>_his" <?php checked($opts['his']); ?>/><?php _e( 'Hide featured image in post details', LANGUAGE_ZONE ); ?></label>
    </p>
    
    <?php
}

/* When the post is saved, saves our custom data */
function thumb_hide_save_postdata( $post_id ) {
    $box_name = 'dt_featured_image_options';
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
          
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !isset( $_POST[$box_name. '_nonce'] ) || !wp_verify_nonce( $_POST[$box_name. '_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    $mydata = array();
    
    $mydata['his'] = isset( $_POST[$box_name. '_his'] );  
    
    update_post_meta( $post_id, $box_name, $mydata );
}

// blog with slider layout metabox
function dt_blog_slider_layout_inner_box( $post ){
    $box_name = 'dt_blog_slider_layout';
        
    $defaults = array(
        'ppp'           => '',
        'orderby'       => 'date',
        'order'         => 'DESC',
    );
    $options = get_post_meta( $post->ID, $box_name, true );
    $options = wp_parse_args( $options, $defaults );
    
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), $box_name. '_nonce' );
    
    ?>
    
    <?php
    // posts_per_page
    dt_admin_ppp_options(
        array(
            'options'   => $options,
            'box_name'  => $box_name
        )
    );
    ?>
    
    <?php
    // print order options
    dt_admin_order_options(
        array(
            'options'   => $options,
            'box_name'  => $box_name
        )
    );
    ?>
    
    <?php
}
			
// blog with slider layout metabox save
function dt_blog_slider_layout_save( $post_id ) {
    $box_name = 'dt_blog_slider_layout';
    
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
  
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !isset( $_POST[$box_name. '_nonce'] ) || !wp_verify_nonce( $_POST[$box_name. '_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    // Check permissions
    if ( !current_user_can( 'edit_page', $post_id ) )
        return;
                    
    $mydata = null;
        
    if( !empty($_POST[$box_name. '_ppp']) ) {
        $mydata['ppp'] = intval($_POST[$box_name. '_ppp']);
    }
    
    if( isset($_POST[$box_name. '_orderby']) ) {
        $mydata['orderby'] = $_POST[$box_name. '_orderby'];
    }
    
    if( isset($_POST[$box_name. '_order']) ) {
        $mydata['order'] = $_POST[$box_name. '_order'];
    }
    
    update_post_meta( $post_id, $box_name, $mydata );
}



  
// thumbnail options box
function dt_post_thumbnail_opts_box( $post ) {
    $box_name = 'dt_post_thumb_opts';
    
    $zc_align = array(
        'c'     => 'Center',
        't'     => 'Top',
        'tr'    => 'Top Right',
        'tl'    => 'Top Left',
        'b'     => 'Bottom',
        'br'    => 'Bottom Right',
        'bl'    => 'Bottom Left',
        'l'     => 'Left',
        'r'     => 'Right'
    );
    
    
    $defaults = array(
        'quality'   => 100,
        'zc'        => true,
        'zc_align'  => 'c',
        'embed'     => ''
    );
    $opts = get_post_meta( $post->ID, $box_name. '_options', true );
    $opts = wp_parse_args( $opts, $defaults );
    
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), $box_name. '_nonce' );
    
    ?>
    <p>
        <label><input type="text" name="<?php echo $box_name; ?>_img_quality" value="<?php echo $opts['quality']; ?>" size="3" /><?php _e( 'Image quality', LANGUAGE_ZONE ); ?> </label>
    </p>
    
    <?php if( 'post' != $post->post_type ): ?>
    <p>
        <label><?php _e( 'Zoom/Cropping', LANGUAGE_ZONE ); ?><input type="checkbox" name="<?php echo $box_name; ?>_img_zc" <?php checked($opts['zc']); ?>/></label>
        <label><select name="<?php echo $box_name; ?>_img_zc_align">
                <?php foreach( $zc_align as $a=>$title ): ?>
                <option value="<?php echo $a; ?>" <?php selected($opts['zc_align'], $a); ?>>
                    <?php echo $title; ?>
                </option>
                <?php endforeach; ?>
            </select><?php _e( 'Z/C align', LANGUAGE_ZONE ); ?></label>
    </p>
    <?php endif; ?>
    
    <?php
    if( 'dt_gallery' != $post->post_type &&
        'dt_our_clients' != $post->post_type ):
    ?>
    
    <p>
        <label><?php _e( 'Embedding link', LANGUAGE_ZONE ); ?><input type="text" name="<?php echo $box_name; ?>_embed" value="<?php echo $opts['embed']; ?>" style="width: 100%;"/></label>
    </p>
    
    <?php endif; ?>
    
    <?php
}

// post thumbnail options save func
function dt_post_thumbnail_opts_save( $post_id ) {
    $box_name = 'dt_post_thumb_opts';
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
  
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !isset( $_POST[$box_name. '_nonce'] ) || !wp_verify_nonce( $_POST[$box_name. '_nonce'], plugin_basename( __FILE__ ) ) )
      return;

  
    // Check permissions
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;

    // OK, we're authenticated: we need to find and save the data
    
    $mydata = array();
    if( isset($_POST[$box_name. '_img_quality']) ) {
        $mydata['quality'] = intval($_POST[$box_name. '_img_quality']);
    }
    
    $mydata['zc'] = isset( $_POST[$box_name. '_img_zc']);
    
    if( isset($_POST[$box_name. '_img_zc_align']) ) {
        $mydata['zc_align'] = esc_attr($_POST[$box_name. '_img_zc_align']);
    }
    
    if( isset($_POST[$box_name. '_embed']) ) {
        $mydata['embed'] = esc_url_raw($_POST[$box_name. '_embed']);
    }
    
    update_post_meta( $post_id, $box_name. '_options', $mydata );
}